<!DOCTYPE html>
<html lang="en">

<head>
    <meta charset="UTF-8">
    <meta name="viewport" content="width=device-width, initial-scale=1.0">
    <title>会员列表</title>
    <link rel="stylesheet" href="https://www.layuicdn.com/layui-v2.5.6/css/layui.css" media="all">
    <style>
        .orderList-container {
            padding: 20px;
        }

        td[data-field="imgUrl"] div,
        td[data-field="negativeImgUrl"] div {
            height: 120px;
        }

        .lay-form {
            padding: 20px;
        }

        .orderList-container {
            background-color: #fff;
        }

        .bar {
            display: flex;
            justify-content: flex-start;
            align-items: flex-end;
        }

        .searchInput {
            display: flex;
            align-items: flex-end;
        }

        .searchInput label {
            font-size: 12px;
        }

        .searchInput input {
            width: 200px;
        }

        .searchInput .layui-form-label {
            padding: 9px 0;
        }

        .bar .layui-form-label {
            padding: 0;
            padding-bottom: 4px;
            text-align: left;
        }

        .bar .layui-inline {
            margin-right: 20px;
        }

        .layui-table img {
            max-width: 200px;
            object-fit: contain;
        }

        .layui-table-cell {
            height: auto;
        }

        .layer-container {
            padding: 20px;
        }

        .layer-container ul {
            display: flex;
        }

        .layer-container ul li {
            border: 1px solid #efefef;
            flex: 1;
            padding: 30px;
            text-align: center;
        }

        .layer-container ul li p {
            line-height: 30px;
            color: #666;
        }

        .layer-container ul .flag {
            flex: 0 0 300px;
        }
    </style>
</head>

<body>
    <div class="orderList-container">
        <!-- 搜索 -->
        <div class="searchInput">
            <form class="layui-form" action="" lay-filter="resets">
                <div class="layui-inline">
                    <label class="layui-form-label">昵称：</label>
                    <input class="layui-input" name="name" placeholder="请输入微信昵称" autocomplete="off">
                </div>
                <div class="layui-inline">
                    <label class="layui-form-label">电话：</label>
                    <input type="text" class="layui-input" id="test1" name="userPhone" placeholder="请输入手机号"
                        autocomplete="off">
                </div>

                 <!-- 筛选 -->
                <div class="layui-inline" id="status">
                    <label class="layui-form-label"
                        style="padding-left:10px; text-align:center; padding-right:0 ; width: 60px;">客户类型：</label>
                    <div class="layui-input-block" style="margin-left: 80px;">
                        <select name="level">
                            <option value="">全部</option>
                            <option value="0">非会员</option>
                            <option value="1">普通会员</option>
                            <option value="2">初级会员</option>
                            <option value="3">中级会员</option>
                            <option value="4">高级会员</option>
                        </select>
                    </div>
                </div>

                <div class="layui-inline">
                    <label class="layui-form-label">注册时间：</label>
                    <input class="layui-input" name="createTime" id="test5" placeholder="注册日期" autocomplete="off">
                </div>

                <button class="layui-btn" data-type="reload" lay-submit="" lay-filter="searchForm">搜索</button>
                <button class="layui-btn layui-btn-primary" data-type="reload" lay-submit=""
                    lay-filter="reset">重置</button>
            </form>
        </div>
        <!-- <button class="layui-btn" id="exports" style="margin-top:20px;">导出全部订单</button> -->
        <table lay-filter="orderListTable" class="layui-hide" id="orderListTable"></table>
    </div>

</body>


<!-- 操作 -->
<!--<script type="text/html" id="barDemo">
    <a class="layui-btn layui-btn-xs" lay-event="lookDetails">发短信</a>
</script>-->
<script src="https://www.layuicdn.com/layui-v2.5.6/layui.js"></script>
<script src="../utils.js"></script>
<script>

    layui.use(['table', 'upload', 'form', 'jquery', "laydate"], function () {
        var table = layui.table,
            upload = layui.upload,
            form = layui.form,
            laydate = layui.laydate,
            $ = layui.jquery;


        table.render({
            elem: '#orderListTable'
            , toolbar: "#barDemo"
            , url: window.baseUrl + '/user/getUserList'
            , headers: {
                Authorization: localStorage.token
            }
            , method: 'post'
            , contentType: 'application/json'
            , response: {
                statusCode: 200 //规定成功的状态码，默认：0
            }
            , cellMinWidth: 80 //全局定义常规单元格的最小宽度，layui 2.2.1 新增
            , parseData: function (res) { //res 即为原始返回的数据
                return {
                    "code": 200,
                    "count": res.data.recordsTotal, //解析数据长度
                    "data": res.data.data //解析数据列表
                };
            }
            , request: {
                pageName: 'start' //页码的参数名称，默认：page
                , limitName: 'length' //每页数据量的参数名，默认：limit
            }
            , cols: [[
                {type:'numbers'},
                {type: 'checkbox'}
                ,{ field: 'name', title: '昵称' }
                , { field: 'sex', title: '性别' ,templet: function(res) {
                    return res.sex == 1 ? "男" : res.sex == 0 ? "女" : "-"
                } }
                , { field: 'age', title: '年龄' ,templet: function(res) {
                    return res.age || "-"
                } }
                , { field: 'userPhone', title: '电话' ,templet: function(res) {
                    return res.userPhone || "-"
                } }
                , {  title: '头像', width:120, templet: function(res) {
                    return res.userPhoto ? "<img class='viewImg' style='width:80px; height:80px;' src='"+ res.userPhoto +"'/>" : "-"
                } }
                , { field: 'level', title: '客户类型',templet: function(res) {
                    return res.level == 0 ? "非会员" : res.level == 1 ? "普通会员" : res.level == 2 ? "初级会员" : res.level == 3 ? "中级会员" : res.level ? "高级会员": '-';
                } }
                , { field: 'shareUserName', title: '推荐人' ,templet: function(res) {
                    return res.shareUserName || "-"
                } }

                , { field: 'shareNum', title: '下级分销人数' }
                , { field: 'benefit', title: '分红收益' }
                , { field: 'benefitAll', title: '总收益' }
                , { field: 'createTime', title: '注册时间' }
            ]],
            page: true
            , done: function () {
                // 放大图片
                $(".viewImg").on("click", function() {
                    let url = $(this).attr("src");
                    layer.open({
                        type: 1,
                        title: false,
                        closeBtn: 0,
                        area: ['auto'],
                        skin: 'layui-layer-nobg', //没有背景色
                        shadeClose: true,
                        content: "<img src=" + url + " />"
                    });
                })

                laydate.render({
                    elem: '#test5'
                });
            }
        });

        

        // 搜索
        form.on("submit(searchForm)", function (e) {

            if (e.field.createTime) {
                e.field.createTime = new Date(e.field.createTime).getTime();
            }

            for (let key in e.field) {
                if (!e.field[key]) {
                    delete e.field[key]
                }
            }

            table.reload('orderListTable', {
                page: {
                    start: 1 //重新从第 1 页开始
                }
                , where: e.field
            });

            return false;
        })


        // 搜索
        form.on("submit(reset)", function (e) {

            table.reload('orderListTable', {
                page: {
                    start: 1 //重新从第 1 页开始
                }
                , where: null
            });

            form.val("resets", {
                name: "",
                userPhone: "",
                createTime: ""
            });

            return false;
        })

        function layerOpen() {
            var checkStatus = table.checkStatus('orderListTable').data;
            layer.open({
                type: 1,
                title: "发短信",
                area: ['560px', '280px'], //宽高
                content: `
                        <form class="layui-form" action="" style="padding:20px;">
                            <div class="layui-form-item layui-form-text">
                                <label class="layui-form-label">短信内容</label>
                                    <div class="layui-input-block">
                                    <textarea style="width: 300px;" name="msg" id="remark" placeholder="请输入短信内容" class="layui-textarea"></textarea>
                                    已选择：${checkStatus.length}人
                                </div>
                            </div>
                            <div class="layui-form-item" >
                                <div class="layui-input-block">
                                    <button type="submit" class="layui-btn" lay-submit="" lay-filter="demo1">立即提交</button>
                                </div>
                            </div>
                        </form>
                    `,
                success: function () {
                    form.render();
                }
            });
        }


        form.on("submit(demo1)", function (el) {

            var checkStatus = table.checkStatus('orderListTable')
            ,data = checkStatus.data;

            if (!data.length) {
                layer.msg(`请选择用户在进行发送短信`)
                return false;
            }

            data.forEach((item, index) => {
                if (!item.userPhone) {
                    layer.msg(`第${(index + 1)}位用户没绑定手机号!`)
                    return false;
                }

                el.field.phone = item.userPhone;
                
                ajax({
                    path: "/user/sendMsg",
                    data: el.field,
                    success: function (res) {
                        if (index == (data.length - 1)) {
                            layer.closeAll();
                            layer.msg("短信发送完成");
                                
                        }
                        
                    }
                }, $)
            })
            
            

            return false;
        })

        

        // 编辑和删除
        table.on("toolbar(orderListTable)", function (data) {
            switch (data.event) {
                case "lookDetails":
                    layerOpen();
                    break;
            }
        })

        // $("#exports").on("click", function(){
        //     ajax({
        //         path: "/distribution/export",
        //         type:"get",
        //         success: function (res) {
        //             console.log(res);
        //         }
        //     }, $)
        // })
    });
</script>

</html>